package com.canaan.business.controller.pc.inOutStock;

import com.canaan.business.common.enums.inOutStock.InStockOrderTypeEnum;
import com.canaan.business.domain.inOutStock.WmsInStockOrder;
import com.canaan.business.domain.vo.*;
import com.canaan.business.service.inOutStock.IWmsInStockOrderService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.utils.bean.BeanUtils;
import com.canaan.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 入库单Controller
 *
 * @author ruoyi
 * @date 2022-12-20
 */
@RestController
@RequestMapping("/inOutStock/inStockOrder")
public class WmsInStockOrderController extends BaseController
{
    @Resource
    private IWmsInStockOrderService wmsInStockOrderService;

    /**
     * 查询入库单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WmsInStockOrder wmsInStockOrder)
    {
        startPage();
        List<WmsInStockOrder> list = wmsInStockOrderService.selectWmsInStockOrderList(wmsInStockOrder);
        return getDataTable(list);
    }

    /**
     * 导出入库单列表
     */
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsInStockOrder wmsInStockOrder)
    {
        List<WmsInStockOrderVo> list = wmsInStockOrderService.exportList(wmsInStockOrder);
        if(InStockOrderTypeEnum.PRODUCTION.getCode().equals(wmsInStockOrder.getOrderType())
            || InStockOrderTypeEnum.STORES_RETURNED.getCode().equals(wmsInStockOrder.getOrderType())
            || InStockOrderTypeEnum.SCRAP.getCode().equals(wmsInStockOrder.getOrderType())){
            //生产入库、退料入库、报废入库
            ExcelUtil<WmsInStockOrderExport1Vo> util = new ExcelUtil<WmsInStockOrderExport1Vo>(WmsInStockOrderExport1Vo.class);
            List<WmsInStockOrderExport1Vo> exportList = BeanUtils.copyList(list,WmsInStockOrderExport1Vo.class);
            String name = InStockOrderTypeEnum.getMessageByCode(wmsInStockOrder.getOrderType());
            util.exportExcel(response, exportList, name + "单数据");
        }else if(InStockOrderTypeEnum.SALES_REFUND.getCode().equals(wmsInStockOrder.getOrderType())){
            //待检入库、收货入库
            ExcelUtil<WmsInStockOrderExport2Vo> util = new ExcelUtil<WmsInStockOrderExport2Vo>(WmsInStockOrderExport2Vo.class);
            List<WmsInStockOrderExport2Vo> exportList = BeanUtils.copyList(list,WmsInStockOrderExport2Vo.class);
            String name = InStockOrderTypeEnum.getMessageByCode(wmsInStockOrder.getOrderType());
            util.exportExcel(response, exportList, name + "单数据");
        }else if(InStockOrderTypeEnum.WAIT_INSPECTION.getCode().equals(wmsInStockOrder.getOrderType())
                || InStockOrderTypeEnum.RECEIVING.getCode().equals(wmsInStockOrder.getOrderType())){
            //调拨入库
            ExcelUtil<WmsInStockOrderExport3Vo> util = new ExcelUtil<WmsInStockOrderExport3Vo>(WmsInStockOrderExport3Vo.class);
            List<WmsInStockOrderExport3Vo> exportList = BeanUtils.copyList(list,WmsInStockOrderExport3Vo.class);
            String name = InStockOrderTypeEnum.getMessageByCode(wmsInStockOrder.getOrderType());
            util.exportExcel(response, exportList, name + "单数据");
        }else if(InStockOrderTypeEnum.ALLOCATE.getCode().equals(wmsInStockOrder.getOrderType())){
            //其他入库
            ExcelUtil<WmsInStockOrderExport4Vo> util = new ExcelUtil<WmsInStockOrderExport4Vo>(WmsInStockOrderExport4Vo.class);
            List<WmsInStockOrderExport4Vo> exportList = BeanUtils.copyList(list,WmsInStockOrderExport4Vo.class);
            String name = InStockOrderTypeEnum.getMessageByCode(wmsInStockOrder.getOrderType());
            util.exportExcel(response, exportList, name + "单数据");
        }else if(InStockOrderTypeEnum.OTHER.getCode().equals(wmsInStockOrder.getOrderType())){
            //销售退货
            ExcelUtil<WmsInStockOrderExport5Vo> util = new ExcelUtil<WmsInStockOrderExport5Vo>(WmsInStockOrderExport5Vo.class);
            List<WmsInStockOrderExport5Vo> exportList = BeanUtils.copyList(list,WmsInStockOrderExport5Vo.class);
            String name = InStockOrderTypeEnum.getMessageByCode(wmsInStockOrder.getOrderType());
            util.exportExcel(response, exportList, name + "单数据");
        }
    }

    /**
     * 获取入库单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsInStockOrderService.selectWmsInStockOrderById(id));
    }

    /**
     * 新增入库单
     */
    @Log(title = "入库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsInStockOrder wmsInStockOrder)
    {
        return toAjax(wmsInStockOrderService.insertWmsInStockOrder(wmsInStockOrder));
    }

    /**
     * 修改入库单
     */
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsInStockOrder wmsInStockOrder)
    {
        return toAjax(wmsInStockOrderService.updateWmsInStockOrder(wmsInStockOrder));
    }

    /**
     * 入库单取消
     */
    @PutMapping("/cancel")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    public AjaxResult cancel(@RequestBody WmsInStockOrder wmsInStockOrder)
    {
        return toAjax(wmsInStockOrderService.cancel(wmsInStockOrder));
    }

    /**
     * 删除入库单
     */
    @Log(title = "入库单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsInStockOrderService.deleteWmsInStockOrderByIds(ids));
    }
}
